const stack = [];

// 入栈
stack.push(1);
stack.push(2);

// 出栈
const item1 = stack.pop();
const item2 = stack.pop();

// JavaScipt实现
class Stack {
  stack = [];

  push(item) {
    this.stack.push(item);
  }
  pop() {
    return this.isEmpty() ? "stack is empty" : this.stack.pop();
  }
  isEmpty() {
    return this.size() === 0;
  }
  clean() {
    this.stack.length = 0;
  }
  size() {
    return this.stack.length;
  }
  peek() {
    // 只是返回元素不会从栈中移除元素
    return this.stack[this.stack.length - 1];
  }
  last() {
    return this.stack[0];
  }
}

// 运行示例
let stackDemo = new Stack();
stackDemo.push(1); // [1]
stackDemo.push(5); // [1, 5]
stackDemo.push(7); // [1, 5, 7]
stackDemo.peek(); // 7
stackDemo.size(); // length 3
stackDemo.last(); // 1

stackDemo.pop(); // [1, 5]
stackDemo.size(); // length 2
stackDemo.pop(); // [1]
stackDemo.clean(); // []
